package com.lry.basic.redis;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class BioServer1 {
    public static void main(String[] args) throws IOException, InterruptedException {
        List<Socket> socketList = new ArrayList<>();
        final ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(9999));
        System.out.println("server start...");
        System.out.println("wait for connect...");
        while(true){
            final Socket socket = serverSocket.accept();//设想不阻塞
            if(socket==null){
                Thread.sleep(1000);
                System.out.println("no one conn");
            }else{
                System.out.println("connect success,客户端ip:"+socket.getInetAddress()+" port:"+socket.getPort());
                socketList.add(socket);
            }

            for (Socket s:socketList) {
                InputStream ins = socket.getInputStream();
                byte[]  bytes = new byte[1024];
                int total = ins.read(bytes);//设想不阻塞
                if(total!=0){
                    System.out.println(new String(bytes));
                }
            }
        }
    }
}
